//
//  VoiceReadItemListView.m
//  ShowYork
//
//  Created by hxh on 2021/5/17.
//  Copyright © 2021 hxh. All rights reserved.
//

#import "VoiceReadItemListView.h"
#import "VoiceReadItemCell.h"
#import "VoiceReadPlayerItemModel.h"
@interface VoiceReadItemListView ()<UITableViewDelegate,UITableViewDataSource,VoiceReadItemCellDelegate>
@property (nonatomic,strong)UIView *containView;

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UIView *headView;
@property (nonatomic,strong)UIView *footView;
@property (nonatomic,copy)NSArray *sourceArray;

@property (nonatomic,strong)UIImageView *sortIcon;
@property (nonatomic,strong)UILabel *titleLb;
@property (nonatomic,strong)UILabel *countLb;

@property (nonatomic,strong)UILabel *cancelLb;

@property (nonatomic,strong)UIButton *sortBtn;

@property (nonatomic,strong)UIButton *cancelBtn;

@property (nonatomic,strong)UIButton *coverBtn;



@end

@implementation VoiceReadItemListView


- (instancetype)initWithSourceArray:(NSArray *)sourceArray{
    if (self = [super init]) {
        self.sourceArray = sourceArray;
        [self setUI];
    }
    return self;
}

- (instancetype)initWithSourceArray:(NSArray *)sourceArray delegate:(id<VoiceReadItemListViewDelegate>)delegate{
    if (self = [super init]) {
        
        self.sourceArray = sourceArray;
        
        self.delegate = delegate;
        
        [self setUI];
    }
    return self;
}


- (void)setUI{
    
    
    self.frame = CGRectMake(0, 0, kScreenW, kScreenH);
    
    self.backgroundColor = RGBA(0, 0, 0, 0.5);
    
    self.coverBtn = [[UIButton alloc]init];
    [self.coverBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.coverBtn];
    [self.coverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self);
    }];
    
    CGFloat maxHeight =  8 * 44 + 49 + GK_SAVEAREA_BTM + 52;
    
    self.containView = [[UIView alloc]init];
    self.containView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.containView];
    
    self.containView.frame = CGRectMake(0,kScreenH, kScreenW, maxHeight);
    self.headView = [[UIView alloc]init];
    
    [self.containView addSubview:self.headView];
    
    [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(52);
        make.left.right.top.equalTo(self.containView);
    }];
    
    self.sortIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"voice_order_tag"]];
    self.sortIcon.highlightedImage = [UIImage imageNamed:@"voice_reverse_tag"];
    [self.headView addSubview:self.sortIcon];
    [self.sortIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.headView);
        make.left.equalTo(self.headView).offset(16);
    }];
    
    self.titleLb = [[UILabel alloc]init];
    self.titleLb.font = [UIFont boldSystemFontOfSize:16];
    self.titleLb.textColor = C51;
    self.titleLb.text = @"顺序播放";
    [self.headView addSubview:self.titleLb];
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.sortIcon.mas_right).offset(8);
        make.centerY.equalTo(self.sortIcon);
    }];
    
    self.countLb = [[UILabel alloc]init];
    self.countLb.font = [UIFont boldSystemFontOfSize:14];
    self.countLb.textColor = C51;
    self.countLb.text = [NSString stringWithFormat:@"(%lu)",self.sourceArray.count];
    [self.headView addSubview:self.countLb];
    [self.countLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLb.mas_right);
        make.centerY.equalTo(self.titleLb);
    }];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.estimatedSectionFooterHeight = .0f;
    self.tableView.estimatedSectionHeaderHeight = .0f;
    self.tableView.estimatedRowHeight = 100;
    self.tableView.separatorColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.scrollEnabled = NO;
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.containView addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.containView);
        make.top.equalTo(self.headView.mas_bottom);
        make.bottom.equalTo(self).offset(-(GK_SAVEAREA_BTM + 49));
    }];
    self.footView = [[UIView alloc]init];
    self.footView.backgroundColor = [UIColor whiteColor];
    [self.containView addSubview:self.footView];
    [self.footView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self.containView);
        make.height.mas_equalTo(GK_SAVEAREA_BTM + 49);
    }];
    
    self.cancelLb = [[UILabel alloc]init];
    self.cancelLb.text = @"取消";
    self.cancelLb.textColor = C51;
    self.cancelLb.textAlignment = NSTextAlignmentCenter;
    self.cancelLb.font = font(18);
    [self.footView addSubview:self.cancelLb];
    [self.cancelLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.footView);
        make.top.equalTo(self.footView).offset(13);
    }];
    
    self.sortBtn = [[UIButton alloc]init];
    [self.sortBtn addTarget:self action:@selector(sortMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.headView addSubview:self.sortBtn];
    [self.sortBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(self.headView);
        make.width.mas_equalTo(150);
    }];
    
    self.cancelBtn = [[UIButton alloc]init];
    [self.cancelBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    [self.footView addSubview:self.cancelBtn];
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.footView);
    }];
    
    if (self.sourceArray.count > 8) {
        self.tableView.scrollEnabled = YES;
        self.tableView.showsVerticalScrollIndicator = NO;
        self.tableView.showsHorizontalScrollIndicator = NO;
    }
    
    NSInteger sortType = [[self.delegate getNewSortType] intValue];
    self.sortIcon.highlighted = sortType == 1;
    self.titleLb.text = sortType == 0?@"顺序播放":@"倒序播放";
}

- (void)sortMethod{
    NSInteger sortType = [[self.delegate getNewSortType] intValue];
    self.sourceArray = [self.sourceArray sortedArrayUsingComparator:^NSComparisonResult(VoiceReadPlayerItemModel *_Nonnull obj1, VoiceReadPlayerItemModel *_Nonnull obj2) {
        return NSOrderedDescending;
    }];
    sortType = sortType == 0?1:0;
    self.sortIcon.highlighted = sortType == 1;
    self.titleLb.text = sortType == 0?@"顺序播放":@"倒序播放";
    if (self.delegate && [self.delegate respondsToSelector:@selector(updateSortType: sourceArray:)]) {
        [self.delegate updateSortType:@(sortType) sourceArray:self.sourceArray];
    }
    [self.tableView reloadData];
}


- (void)dismiss{
    
    self.backgroundColor = [UIColor clearColor];
    
    [UIView animateWithDuration:0.25 animations:^{
        self.cl_y = kScreenH;
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(dismissListView)]) {
        [self.delegate dismissListView];
    }
}

- (void)show{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:0.25 animations:^{
        self.containView.cl_bottom = kScreenH;
    } completion:^(BOOL finished) {
        
    }];
}
- (void)updateListViewStatus{
    [self.tableView reloadData];
}

- (void)reloadDataWithIndex:(NSInteger)index{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
    [self.tableView reloadRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark - tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.sourceArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    VoiceReadItemCell *cell = [VoiceReadItemCell cellWithTableView:tableView];
    cell.delegate = self;
    [cell setItemModel:self.sourceArray[indexPath.item]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01f;
}

- (void)playWithCurrentModel:(VoiceReadPlayerItemModel *)itemModel{
    NSInteger index = [self.sourceArray indexOfObject:itemModel];
    if (self.delegate && [self.delegate respondsToSelector:@selector(playCurrentVoiceByIndex:)]) {
        [self.delegate playCurrentVoiceByIndex:index];
    }
}
- (void)continueWithCurrentModel:(VoiceReadPlayerItemModel *)itemModel{
    NSInteger index = [self.sourceArray indexOfObject:itemModel];
    if (self.delegate && [self.delegate respondsToSelector:@selector(continueCurrentVoiceByIndex:)]) {
        [self.delegate continueCurrentVoiceByIndex:index];
    }
}
- (void)pauseWithCurrentModel:(VoiceReadPlayerItemModel *)itemModel{
    NSInteger index = [self.sourceArray indexOfObject:itemModel];
    if (self.delegate && [self.delegate respondsToSelector:@selector(pauseCurrentVoiceByIndex:)]) {
        [self.delegate pauseCurrentVoiceByIndex:index];
    }
}


- (void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.containView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(14, 14)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.containView.bounds;
    maskLayer.path = maskPath.CGPath;
    self.containView.layer.mask = maskLayer;
}

@end
